package com.axend.aerosense.common.bean;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import com.axend.aerosense.base.app.BaseApplication;
import com.axend.aerosense.base.bean.ChooseDeviceBean;
import com.axend.aerosense.base.database.device.DeviceInfoManager;
import com.axend.aerosense.common.R;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.UriUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public enum RadarType {
    //防摔雷达
    ASSURE(0, R.string.common_aerosense_assure, R.mipmap.common_assure_icon, (byte) 0x00),
    //呼吸心率雷达
    WAVVE(1, R.string.common_aerosense_wavve, R.mipmap.common_wavve_icon, (byte) 0x13),
    //assure lite雷达
    ASSURE_LITE(2, R.string.common_aerosense_assure_lite, R.mipmap.common_wavve_icon, (byte) 0x14),
    //wavve pro雷达
    WAVVE_PRO(3, R.string.common_aerosense_wavve_pro, R.mipmap.common_wavve_icon, (byte) 0x00),
    ;

    private int type;       //类型（与服务器下发的对应）
    private int name;       //设备名字
    private int imageRes;   //图片资源
    private byte magic;     //魔术位（与设备通信的时候使用）

    private static Map<Integer, RadarType> typeMap;
    private static Map<String, RadarType> nameMap;
    private static Map<Byte, RadarType> magicMap;

    static {
        typeMap = new HashMap<>();
        nameMap = new HashMap<>();
        magicMap = new HashMap<>();
        for (RadarType r : RadarType.values()) {
            typeMap.put(r.get(), r);
            nameMap.put(r.getNameByString().toUpperCase(), r);
            magicMap.put(r.magic, r);
        }
    }

    RadarType(int type, int name, int imageRes, byte magic) {
        this.type = type;
        this.name = name;
        this.imageRes = imageRes;
        this.magic = magic;
    }

    public int get() {
        return type;
    }

    public int getName() {
        return name;
    }

    public byte getMagic() {
        return magic;
    }

    public String getNameByString() {
        return BaseApplication.getInstance()
                .getApplicationContext()
                .getString(name);
    }

    public int getImageRes() {
        return imageRes;
    }

    public static RadarType getType(int type) {
        return typeMap.get(type);
    }

    /**
     * 通过名字获取设备类型
     */
    public static RadarType getType(String name) {
        if (TextUtils.isEmpty(name)) {
            return null;
        }
        return nameMap.get(name.toUpperCase());
    }

    /**
     * 通过魔术位，获取雷达类型
     */
    public static RadarType getMagic(byte b) {

        return magicMap.get(b);
    }


    /**
     * 设置雷达图片
     * @param context
     * @param view
     * @param radarType
     */
    public static void setRadarImageRes(Context context, ImageView view, RadarType radarType) {
        ChooseDeviceBean.DeviceInfo deviceInfo = DeviceInfoManager.getInstance(context).deviceInfoDao().queryDeviceType(context.getString(radarType.getName()).toUpperCase());
        if (deviceInfo != null)
            view.setImageURI(Uri.parse(deviceInfo.getFileUri()));
        else
            view.setImageResource(radarType.getImageRes());
    }
}
